<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Videos extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function candidate($candidate_id = null, $candidate_name = null)
	{
		$this->lang->load('candidate/profile', $this->session->userdata['user_lang']);
		$this->lang->load('candidate/videos', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_model', 'candidate');
		$this->load->helper('date');
		
		$this->get_subscription_menu($data, null, $candidate_id);
		
		$data['main_content'] = 'candidate/profile/videos';
        $data['userdata'] = $this->session->userdata;
        
        $data['candidate'] = $this->candidate->get_complete_profile(array('idcandidate' => $candidate_id));
        $data['profile_image'] = $data['candidate']->image;
        
        $data['grid_videos'] = $this->get_candidate_videos($data['candidate']->idmember);
		
        $data['page_title'] = sprintf($this->lang->line('videos_profilevideos'), $data['candidate']->firstname . ' ' . $data['candidate']->lastname);
        $this->load->view('template-candidate-profile', $data);
	}
	
	private function get_candidate_videos($member_id)
	{
		$this->lang->load('candidate/video', $this->session->userdata['user_lang']);
		$this->load->model('Candidate_video_model', 'video');
		
		$videos_list = $this->video->get(array('idmember' => $member_id));
		
		$grid_videos = array();
        
		foreach ($videos_list->result() as $row)
		{
			$type = $row->idopentok == 'Y' ? 'opentok' : 'youtube';
			$highlight = $row->idpresentation == 'Y' ? ' row-highlight' : '';
			$title_additional = $row->idpresentation == 'Y' ? (' (' . $this->lang->line('video_presentation') . ')') : '';
			
			$video = '<div class="row'.$highlight.'"><span><span class="img-galery"><span class="img-border fleft vmiddle">';
			
			if ($row->idopentok != 'Y')
			{
				$video .= '<img class="pointer" width="160" height="120" onclick="CandidateVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');" src="http://img.youtube.com/vi/'.$row->link.'/0.jpg" />';
			}
			else
			{
				$video .= '<img class="pointer" onclick="CandidateVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');" src="'.base_url() . "f/" . $member_id . '/vid/' . $row->image.'" />';
			}
			
			$video .= '</span></span><span class="fleft vmiddle box-description"><h3>'.$row->title . $title_additional .'</h3><p>'.nl2br($row->description).'</p></span>
			</span>
			<div class="gtool">
			<a onclick="CandidateVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');">'.$this->lang->line('video_watchvideo').'</a></div></div>';
			
			$grid_videos[] = $video;
		}
		
		if (!$grid_videos) {
			 return '<center>'.$this->lang->line('video_noresults').'</center>';
		} else {
			 return implode('<hr/>', $grid_videos);
		}
	}
	
	public function company($company_id = null, $company_name = null)
	{
		$this->lang->load('company/profile', $this->session->userdata['user_lang']);
		$this->lang->load('company/videos', $this->session->userdata['user_lang']);
		$this->load->model('Company_model', 'company');
		$this->load->helper('date');
	
		//$this->get_subscription_menu($data, null, $candidate_id);
	
		$data['main_content'] = 'company/profile/videos';
		$data['userdata'] = $this->session->userdata;
	
		$data['company'] = $this->company->get_complete_profile(array('idcompany' => $company_id));
		$data['profile_image'] = $data['company']->image;
	
		$data['grid_videos'] = $this->get_company_videos($data['company']->idmember);
	
		$data['page_title'] = sprintf($this->lang->line('videos_profilevideos'), $data['company']->name);
		$this->load->view('template-company-profile', $data);
	}
	
	private function get_company_videos($member_id)
	{
		$this->lang->load('company/video', $this->session->userdata['user_lang']);
		$this->load->model('Company_video_model', 'video');
	
		$videos_list = $this->video->get(array('idmember' => $member_id));
	
		$grid_videos = array();
	
		foreach ($videos_list->result() as $row)
		{
			$type = $row->idopentok == 'Y' ? 'opentok' : 'youtube';
			$highlight = $row->idpresentation == 'Y' ? ' row-highlight' : '';
			$title_additional = $row->idpresentation == 'Y' ? (' (' . $this->lang->line('video_presentation') . ')') : '';
				
			$video = '<div class="row'.$highlight.'"><span><span class="img-galery"><span class="img-border fleft vmiddle">';
				
			if ($row->idopentok != 'Y')
			{
				$video .= '<img class="pointer" width="160" height="120" onclick="CompanyVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');" src="http://img.youtube.com/vi/'.$row->link.'/0.jpg" />';
			}
			else
			{
				$video .= '<img class="pointer" onclick="CompanyVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');" src="'.base_url() . "f/" . $member_id . '/vid/' . $row->image.'" />';
			}
				
			$video .= '</span></span><span class="fleft vmiddle box-description"><h3>'.$row->title . $title_additional .'</h3><p>'.nl2br($row->description).'</p></span>
			</span>
			<div class="gtool">
			<a onclick="CompanyVideo.watchVideo(\''.$type.'\', \''.$row->link.'\');">'.$this->lang->line('video_watchvideo').'</a></div></div>';
				
			$grid_videos[] = $video;
		}
	
		if (!$grid_videos) {
			return '<center>'.$this->lang->line('video_noresults').'</center>';
		} else {
			return implode('<hr/>', $grid_videos);
		}
	}
}

?>